<template>
  <ych-modal
    :fullscreen="false"
    size="middle"
    :title="title"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :cancelText="$t('m.common.button.close')">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :label="$t('m.system.oauth2Manage.entity.clientId')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['clientId', validatorRules.clientId]"
                   :placeholder="$t('m.system.oauth2Manage.verify.clientId')" :disabled="!!model.id"></a-input>
        </a-form-item>
        <a-form-item :label="$t('m.system.oauth2Manage.entity.clientSecretType')" :labelCol="labelCol"
                     :wrapperCol="wrapperCol" v-if="!model.id">
          <a-radio-group buttonStyle="solid" v-decorator="['clientSecretType', validatorRules.clientSecretType]">
            <a-radio-button value="bcrypt">{{ $t('m.system.oauth2Manage.other.bcrypt') }}</a-radio-button>
            <a-radio-button value="noop">{{ $t('m.system.oauth2Manage.other.noop') }}</a-radio-button>
          </a-radio-group>
        </a-form-item>
        <a-form-item :label="$t('m.system.oauth2Manage.entity.clientSecret')" :labelCol="labelCol"
                     :wrapperCol="wrapperCol">
          <a-input v-decorator="['clientSecret', validatorRules.clientSecret]"
                   :placeholder="$t('m.system.oauth2Manage.verify.clientSecret')" :disabled="!!model.id"></a-input>
        </a-form-item>
        <a-form-item :label="$t('m.system.oauth2Manage.entity.clientName')" :labelCol="labelCol"
                     :wrapperCol="wrapperCol">
          <a-input v-decorator="['clientName', validatorRules.clientName]"
                   :placeholder="$t('m.system.oauth2Manage.verify.clientName')"></a-input>
        </a-form-item>
        <a-form-item :label="$t('m.system.oauth2Manage.entity.clientAuthenticationMethods')" :labelCol="labelCol"
                     :wrapperCol="wrapperCol">
          <a-select v-decorator="['clientAuthenticationMethods', validatorRules.clientAuthenticationMethods]">
            <a-select-option value="client_secret_basic">client_secret_basic</a-select-option>
            <a-select-option value="client_secret_post">client_secret_post</a-select-option>
            <a-select-option value="client_secret_jwt">client_secret_jwt</a-select-option>
            <a-select-option value="private_key_jwt">private_key_jwt</a-select-option>
            <a-select-option value="none">none</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :label="$t('m.system.oauth2Manage.entity.redirectUris')" :labelCol="labelCol"
                     :wrapperCol="wrapperCol">
          <a-input v-decorator="['redirectUris', validatorRules.redirectUris]"
                   :placeholder="$t('m.system.oauth2Manage.verify.redirectUris')"></a-input>
        </a-form-item>
        <a-form-item :label="$t('m.system.oauth2Manage.entity.scopes')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['scopes', validatorRules.scopes]"
                   :placeholder="$t('m.system.oauth2Manage.verify.scopes')"></a-input>
        </a-form-item>
      </a-form>
    </a-spin>
  </ych-modal>
</template>

<script>

import {getAction, httpAction} from '@/api/manage'
import YchConstant from "@/api/YchConstant";

export default {
  name: "SysOauth2Modal",
  data() {
    return {
      form: this.$form.createForm(this),
      title: this.$t('m.system.oauth2Manage.entity.title'),
      visible: false,
      model: {},
      labelCol: {
        xs: {span: 24},
        sm: {span: 6},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 18},
      },
      confirmLoading: false,
      validatorRules: {
        clientId: {
          rules: [
            {required: true, message: this.$t('m.system.oauth2Manage.verify.clientId')},
          ]
        },
        clientSecret: {
          rules: [
            {required: true, message: this.$t('m.system.oauth2Manage.verify.clientSecret')},
          ]
        },
        clientName: {
          rules: [
            {required: true, message: this.$t('m.system.oauth2Manage.verify.clientName')},
          ]
        },
        clientSecretType: {
          initialValue: 'noop'
        },
        clientAuthenticationMethods: {
          rules: [
            {required: true, message: this.$t('m.system.oauth2Manage.verify.clientAuthenticationMethods')},
          ],
          initialValue: 'client_secret_post'
        },
        redirectUris: {
          rules: [
            {required: true, message: this.$t('m.system.oauth2Manage.verify.redirectUris')},
          ]
        },
        scopes: {
          rules: [
            {required: true, message: this.$t('m.system.oauth2Manage.verify.scopes')},
          ],
          initialValue: 'openid'
        },
      },
      url: {
        queryById: YchConstant.PREFIX_SYSTEM + "/oauth2/queryById",
        add: YchConstant.PREFIX_SYSTEM + "/oauth2/add",
        edit: YchConstant.PREFIX_SYSTEM + "/oauth2/edit",
      }
    }
  },
  methods: {
    add() {
      this.edit({});
    },
    edit(record) {
      this.form.resetFields();
      if (record.id) {
        this.confirmLoading = true;
        getAction(this.url.queryById, {id: record.id}).then((res) => {
          if (res.success) {
            this.initFormData(res.result)
          } else {
            this.messageUtil.warning(res.message)
          }
        }).finally(() => {
          this.confirmLoading = false;
        })
      } else {
        this.initFormData(record)
      }
    },
    initFormData(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(this.lodash.pick(this.model, 'clientId', 'clientSecret', 'clientName', "redirectUris", "scopes", "clientAuthenticationMethods"))
      })
    },
    close() {
      this.$emit('close');
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          let formData = Object.assign(this.model, values);
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.messageUtil.success(res.message);
              that.$emit('ok');
            } else {
              that.messageUtil.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.close();
          })
        }
      })
    },
    handleCancel() {
      this.close()
    },
  }
}
</script>
